package com.qiaofang.housewarehouse.facade.impl;

import com.qiaofang.housewarehouse.dao.domain.HswhRentalHouse;
import com.qiaofang.housewarehouse.dao.utils.ConfigUtils;
import com.qiaofang.housewarehouse.service.RentalHouseQueryService;
import com.qiaofang.housewarehouse.stub.constants.HouseTypeEnum;
import com.qiaofang.housewarehouse.stub.constants.ResponseCode;
import com.qiaofang.housewarehouse.stub.constants.TableConstants;
import com.qiaofang.housewarehouse.stub.facade.RentalHouseQueryFacade;
import com.qiaofang.housewarehouse.stub.request.HouseQueryDetailRequest;
import com.qiaofang.housewarehouse.stub.request.RentalHouseQueryRequest;
import com.qiaofang.housewarehouse.stub.response.RentalHouseQueryDetailResponse;
import com.qiaofang.housewarehouse.stub.response.RentalHouseQueryResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 * 出租房源接口实现
 */
@RestController
public class RentalHouseQueryFacadeImpl implements RentalHouseQueryFacade {
    private static final Logger logger = LoggerFactory.getLogger(RentalHouseQueryFacadeImpl.class);

    @Resource
    private RentalHouseQueryService rentalHouseQueryService;

    /**
     * 出租房源列表查询
     *
     * @param rentalHouseQueryRequest
     * @return
     */
    @Override
    public RentalHouseQueryResponse searchRentalHouseList(@RequestBody @Valid RentalHouseQueryRequest rentalHouseQueryRequest) {
        logger.info("查询出租房源列表开始");
        RentalHouseQueryResponse response = new RentalHouseQueryResponse();
        try {
            Map<String, String> paramMap = new HashMap<>();
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getPageIndex())) {
                paramMap.put("pageIndex", rentalHouseQueryRequest.getPageIndex());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getPageSize())) {
                paramMap.put("pageSize", rentalHouseQueryRequest.getPageSize());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getContactMobiles())) {
                paramMap.put("contactMobiles", rentalHouseQueryRequest.getContactMobiles());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getEstates())) {
                paramMap.put("estates",rentalHouseQueryRequest.getEstates());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getCity())) {
                paramMap.put("city", rentalHouseQueryRequest.getCity());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getKeywords())) {
                paramMap.put("keywords", rentalHouseQueryRequest.getKeywords());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getRegion())) {
                paramMap.put("region", rentalHouseQueryRequest.getRegion());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getDistrict())) {
                paramMap.put("district", rentalHouseQueryRequest.getDistrict());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getDecoration())) {
                paramMap.put("decoration", rentalHouseQueryRequest.getDecoration());
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getHouseType())) {
                String houseType =  rentalHouseQueryRequest.getHouseType();
                Map<String, Integer> stringIntegerMap = HouseTypeEnum.toMap();
                for (String key : stringIntegerMap.keySet() ){
                    if(houseType.contains(key)){
                        paramMap.put("houseType", String.valueOf(stringIntegerMap.get(key)));
                    }
                }
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getStartRentPrice()) || StringUtils.isNotBlank(rentalHouseQueryRequest.getEndRentPrice())) {
                paramMap.put("rentPriceRange", (StringUtils.isNotBlank(rentalHouseQueryRequest.getStartRentPrice()) ? rentalHouseQueryRequest.getStartRentPrice() : 0)
                        + "~" +  (StringUtils.isNotBlank(rentalHouseQueryRequest.getEndRentPrice()) ? rentalHouseQueryRequest.getEndRentPrice() : 0));
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getStartRoomArea()) || StringUtils.isNotBlank(rentalHouseQueryRequest.getEndRoomArea())) {
                paramMap.put("roomAreaRange", (StringUtils.isNotBlank(rentalHouseQueryRequest.getStartRoomArea()) ? rentalHouseQueryRequest.getStartRoomArea() : 0)
                        + "~" + (StringUtils.isNotBlank(rentalHouseQueryRequest.getEndRoomArea()) ? rentalHouseQueryRequest.getEndRoomArea() : 0));
            }
            if (StringUtils.isNotBlank(rentalHouseQueryRequest.getStartFloor()) || StringUtils.isNotBlank(rentalHouseQueryRequest.getEndFloor())) {
                paramMap.put("floorRange", (StringUtils.isNotBlank(rentalHouseQueryRequest.getStartFloor()) ? rentalHouseQueryRequest.getStartFloor() : 0)
                        + "~" + (StringUtils.isNotBlank(rentalHouseQueryRequest.getEndFloor()) ? rentalHouseQueryRequest.getEndFloor() : 0));
            }
            logger.info("接受参数值：\n");
            for (String ks : paramMap.keySet()) {
                logger.info("key=" + ks + "," + "value=" + paramMap.get(ks) + "\t");
            }
            response.setData(rentalHouseQueryService.searchRentalHouseList(paramMap));
            response.setResponseCode(ResponseCode.RC_SUCCESS.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_SUCCESS.getResponseMessage());
            logger.info("查询出租房源结束====SUCCESS");
        } catch (Throwable e) {
            response.setResponseCode(ResponseCode.RC_EXCEPTION_ERROR.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_EXCEPTION_ERROR.getResponseMessage());
            logger.error("RentalHouseQueryFacadeImpl error！", e);
        }
        return response;
    }

    /**
     * 出租房源明细查询
     *
     * @param rentalHouseQueryDetailRequest
     * @return
     */
    @Override
    public RentalHouseQueryDetailResponse getRentalHouseDetail(@RequestBody @Valid HouseQueryDetailRequest rentalHouseQueryDetailRequest) {
        RentalHouseQueryDetailResponse response = new RentalHouseQueryDetailResponse();
        try {
            logger.info("查询出租房源明细开始");
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("indexName", ConfigUtils.getIndexHswhRentalHouse());
            paramMap.put("typeName", TableConstants.INDEX_TYPE_RENTAL_HOUSE);
            response.setData(rentalHouseQueryService.getRentalHouseDetail(paramMap,TableConstants.HSWH_RENTAL_HOUSE, rentalHouseQueryDetailRequest.getId(), HswhRentalHouse.class));
            response.setResponseCode(ResponseCode.RC_SUCCESS.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_SUCCESS.getResponseMessage());
            logger.info("查询出租房源明细结束=====SUCCESS");
        } catch (Exception ex) {
            response.setResponseCode(ResponseCode.RC_EXCEPTION_ERROR.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_EXCEPTION_ERROR.getResponseMessage());
            logger.error("RentalHouseQueryFacadeImpl error！", ex);
        }
        return response;
    }


}
